package com.itheima.interceptor;

import com.itheima.utils.JwtUtils;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import java.util.Map;

@Component
@Slf4j
public class LoginCheckInterceptor implements HandlerInterceptor {
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("================================"+request.getRequestURI());
        String jwt = request.getHeader("token");
        try {
            Map<String, Object> map = JwtUtils.parseJwt(jwt);
            return true;
        } catch (Exception e) {
            log.error("jwt校验失败，非法请求:{}",jwt);
            response.setStatus(401);
            return false;
        }
    }
}
